<html>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.2.3/milligram.min.css">
  </head>
  <body class="container" style="padding-top: 3%;">
    <hr />

    <div class="row">
      <div class="column column-50">
        <label>当前账号：<span id="connaddr"></span>，余额:<span id="myeth"></span></label>
        <input id="contact" type="text" placeholder="合约地址" />
        <!-- <input id="valueAmount" type="text" placeholder="数量" />
        <input id="addr" type="text" placeholder="地址" /> -->

        <div>
          <button id="contactInit">初始化合约</button>
          <button id="setValue">合约名称</button>
          <button id="getValue">发行量</button>
          
          <button id="blanacel">合约总量</button>
          <button id="base">盛世宝藏</button>
          <button id="sign">签约池</button>
          <button id="gj">冠军池</button>
          <button id="jj">溢出池</button>

          <button id="kjs">可结算收益</button>
          <button id="n20">溢出结果</button>
          <button id="zhit">当前直推前三</button>
          <button id="zuori">昨日直推前三</button>
          <button id="rewd">中奖名单20</button>
          <button id="myzht1">我的账户信息1</button>
          <button id="myzht2">我的账户信息2</button>
          <button id="mylow">我的投入</button>

          <button id="vip1">1级VIP地址</button>
          <button id="vip2">2级VIP地址</button>
          <button id="vip3">3级VIP地址</button>
      </div>
      <div>
        <button id="settle" style="background-color: blue;">结算</button>
      </div>

        <div class="row row-center">
          <input id="addr_2" type="text" placeholder="地址" />
          <button id="btn_2">余额</button>
        </div>

        <div class="row row-center">
          <input id="addr_4" type="number" placeholder="数量" />
          <button id="btn_4" style="background-color: blue;">提现</button>
        </div>

        <div class="row row-center">
          <input id="addr_6" type="text" placeholder="上级地址" />
          <input id="amt_6" type="number" placeholder="数量" style="width:200px"/>
          <button id="btn_6" style="background-color: blue;">签约</button>
        </div>
        

        <br /><br />

      </div>
      <div class="column column-50">
        <label id="desc"></label>
        <!-- <input id="bytecode" type="text" value="" />

        <label>ABI</label>
        <textarea id="abi"></textarea> -->
        
        <div id="response" style="height:620px;width:600px;padding: 20px;overflow: auto; border: 1px solid #9b4dca; word-wrap: break-word">
          
        </div>
      </div>
    </div>

    <br /><br />

    <script type="text/javascript" src="ethjs.js"></script>
    <script type="text/javascript" src="ethjs-unit.js"></script>
    <script type="text/javascript" src="ethjs-util.js"></script>
    <script type="text/javascript">
      var eth = new Eth(web3.currentProvider);
      var el = function(id){ return document.querySelector(id); };

      window.onload = function(){
        SimpleStore = eth.contract(SimpleStoreABI).at(el("#contact").value);
        el('#response').innerHTML = "初始化成功";
      }

      var SimpleStoreABI = [{"constant":true,"inputs":[],"name":"caclSettle","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"initbase","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"value","type":"uint256"}],"name":"callowner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v20","outputs":[{"name":"addr","type":"address"},{"name":"w9","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"settle","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"}],"name":"randReward","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"shv","outputs":[{"name":"v11","type":"uint8"},{"name":"v13","type":"uint8"},{"name":"v14","type":"uint8"},{"name":"vip1","type":"uint8"},{"name":"vip2","type":"uint8"},{"name":"vip3","type":"uint8"},{"name":"v16","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"parent","type":"address"}],"name":"join","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v21","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"caclWing","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v22_vip3","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v22_vip2","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v5","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"v22","outputs":[{"name":"v1","type":"uint256"},{"name":"v3","type":"uint256"},{"name":"v4","type":"uint256"},{"name":"v5","type":"uint256"},{"name":"v6","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v8","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v23","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"store2_","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"caclsub","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"store1","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"rewardlevel","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"value","type":"uint256"}],"name":"initstore","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"addr","type":"address"},{"name":"i","type":"uint256"}],"name":"findLevelAddr","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v22_vip1","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"addr","type":"address"}],"name":"caclAlllevel","outputs":[{"name":"","type":"address[15]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"cur","type":"address"},{"name":"amt","type":"uint256"}],"name":"initFirst","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"sender","type":"address"},{"name":"val","type":"uint256"}],"name":"initOut","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"v4","outputs":[{"name":"v1","type":"uint256"},{"name":"v2","type":"uint256"},{"name":"v13","type":"uint256"},{"name":"v3","type":"address"},{"name":"v4","type":"bool"},{"name":"v5","type":"uint256"},{"name":"v6","type":"uint256"},{"name":"v7","type":"uint256"},{"name":"v8","type":"uint256"},{"name":"v9","type":"uint256"},{"name":"v10","type":"uint256"},{"name":"v11","type":"uint256"},{"name":"v12","type":"uint256"},{"name":"v14","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v16","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"store2","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"isRevel","type":"bool"}],"name":"callBaseSend","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v17","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"p1","outputs":[{"name":"v1","type":"address"},{"name":"v3","type":"uint256"},{"name":"v4","type":"uint256"},{"name":"v9","type":"uint256"},{"name":"v12","type":"uint256"},{"name":"v10","type":"uint256"},{"name":"v11","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"val","type":"uint256"}],"name":"caclDayAmt","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"m0","type":"uint256"}],"name":"drawblock","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v24","outputs":[{"name":"addr","type":"address"},{"name":"w9","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"mylow","outputs":[{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v23_amt","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"store2_n","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"},{"name":"vnext_15","type":"address[15]"}],"name":"caclLevel","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v19","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"v21_amt","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"i","type":"uint256"},{"name":"cur","type":"address"},{"name":"amt","type":"uint256"}],"name":"initVip","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"vipcacl","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"tl","type":"address"}],"payable":true,"stateMutability":"payable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"}];
      var SimpleStore;
      el("#contactInit").addEventListener('click', function(){
          SimpleStore = eth.contract(SimpleStoreABI).at(el("#contact").value);
          el('#response').innerHTML = "初始化成功";
      });
      

      el('#blanacel').addEventListener('click', function(){
        var contact = el("#contact").value;
        eth.getBalance(contact).then(function(valueStored){
          el('#response').innerHTML = String(ethUnit.fromWei(valueStored,'ether'));
        });
      });

      el('#base').addEventListener('click', function(){
        el("#desc").innerHTML = "基金池";
        SimpleStore.store2().then(function(setTxHash){
          var e1 = ethUnit.fromWei(setTxHash[0],'ether');
          el('#response').innerHTML = '数量: ' + String(e1);
        });
      });


      el('#sign').addEventListener('click', function(){
        SimpleStore.p1().then(function(valueStored){
          el("#desc").innerHTML = "签约池";
          var html = "总币数:"+String(ethUnit.fromWei(valueStored[1],'ether'))+"<br/>";
          el('#response').innerHTML = html;
        });
      });

      el('#gj').addEventListener('click', function(){
        SimpleStore.store1().then(function(setTxHash){
          el("#desc").innerHTML = "冠军池";
          el('#response').innerHTML = '数量: ' + String(ethUnit.fromWei(setTxHash[0],'ether'));
        });
      });

      el('#jj').addEventListener('click', function(){
        el("#desc").innerHTML = "溢出池";
        SimpleStore.store2().then(function(setTxHash){
          var e1 = ethUnit.fromWei(setTxHash[0],'ether');
          SimpleStore.store2_n().then(function(setTxHash){
            var e2 = ethUnit.fromWei(setTxHash[0],'ether');
            el('#response').innerHTML = '数量: ' + String(e1-e2);
          });
        });
      });

      el('#myzht1').addEventListener('click', function(){
        el("#desc").innerHTML = "我的账户信息";
          eth.accounts().then(function(accounts) {
            SimpleStore.v22(accounts[0]).then(function(valueStored,error){
              var vv = String(ethUnit.fromWei(valueStored[1],'ether'));
              el('#response').innerHTML = "出局数量："+vv+"<br/>";

              vv = String(ethUnit.fromWei(valueStored[2],'ether'));
              el('#response').innerHTML += "推广收益："+vv+"<br/>";

              vv = String(ethUnit.fromWei(valueStored[3],'ether'));
              el('#response').innerHTML += "已结算收益："+vv+"<br/>";

              vv = String(ethUnit.fromWei(valueStored[4],'ether'));
              el('#response').innerHTML += "总签约量："+vv+"<br/>";

              var len = String(valueStored[0]);
              el('#response').innerHTML += "直推数："+len+"<br/>";

              for(var x=0;x<len;x++){
                SimpleStore.findLevelAddr(accounts[0],x).then(function(valueStored,error){
                  var len = String(valueStored[0]);
                  el('#response').innerHTML += len+"<br/>";
                });
              }
            });
          });
      });

      el('#myzht2').addEventListener('click', function(){
        el("#desc").innerHTML = "我的账户信息";
        eth.accounts().then(function(accounts) {
          SimpleStore.v4(accounts[0]).then(function(valueStored){
            var html = "";
            for(var val in valueStored){
              var l = val;
              if(l=='v1'){
                break;
              }
              if(val==0)l="签约数量";
              if(val==1)l="获得币数";
              if(val==2)l="可提币数";
              if(val==3)l="上级";
              if(val==4)l="是否有效";
              if(val==5)l="充币时间";
              if(val==6)l="上次结算时间";
              if(val==7)l="索引";
              if(val==8)l="直推总币数";
              if(val==9)l="15代小区总币数";
              if(val==10)l="中奖数量";
              if(val==11)l="抽奖次数";
              if(val==12)l="股东等级";
              if(val==13)l="已派发奖的等级";
              if(val==0||val==1||val==2||val==8||val==9||val==10){
                html += l + ":"+ ethUnit.fromWei(valueStored[val],'ether') +"<br/>";
              }else{
                html += l + ":"+ valueStored[val] +"<br/>";
              }
            }
            el('#response').innerHTML = html;
          });
        });
      });

      el('#rewd').addEventListener('click', function(){
        el('#response').innerHTML = '';
        for(var x=0;x<20;x++){
          SimpleStore.v24(x).then(function(setTxHash){
            if(!isZero(setTxHash[0])){
              el('#response').innerHTML += setTxHash[0] +"："+ethUnit.fromWei(setTxHash[1],'ether') +"<br/>";
            }
          });
        }
      });
//========================

      el('#vip1').addEventListener('click', function(){
          el('#response').innerHTML = "";
          el("#desc").innerHTML = "查看1级VIP地址";
          SimpleStore.shv().then(function(valueStored){
            var vip1 = String(valueStored[4]);
            var v1 = parseInt(vip1);
            if(v1==0){
              el('#response').innerHTML = "没有VIP1股东地址<br/>";
            }
            showvip1(0,v1);
          });
      });

      function showvip1(i,v1){
        if(i>v1){
          return;
        }
        SimpleStore.v22_vip1(i).then(function(setTxHash){
          for(var val in setTxHash){
            el('#response').innerHTML += val + ":"+ setTxHash[val] +"<br/>";
            showvip1(++i,v1);
          }
        });
      }

      el('#vip2').addEventListener('click', function(){
          el('#response').innerHTML = "";
          el("#desc").innerHTML = "查看2级VIP地址";
          SimpleStore.shv().then(function(valueStored){
            var vip = String(valueStored[5]);
            var v1 = parseInt(vip);
            if(v1==0){
              el('#response').innerHTML = "没有VIP2股东地址<br/>";
            }
            showvip2(0,v1);
          });
      });

      function showvip2(i,v1){
        if(i>v1){
          return;
        }
        SimpleStore.v22_vip2(i).then(function(setTxHash){
          for(var val in setTxHash){
            el('#response').innerHTML += val + ":"+ setTxHash[val] +"<br/>";
            showvip2(++i,v1);
          }
        });
      }

      el('#vip3').addEventListener('click', function(){
          el('#response').innerHTML = "";
          el("#desc").innerHTML = "查看3级VIP地址";
          SimpleStore.shv().then(function(valueStored){
            var vip = String(valueStored[6]);
            var v1 = parseInt(vip);
            if(v1==0){
              el('#response').innerHTML = "没有VIP3股东地址<br/>";
            }
            showvip3(0,v1);
          });
      });

      function showvip3(i,v1){
        if(i>v1){
          return;
        }
        SimpleStore.v22_vip3(i).then(function(setTxHash){
          for(var val in setTxHash){
            el('#response').innerHTML += val + ":"+ setTxHash[val] +"<br/>";
            showvip3(++i,v1);
          }
        });
      }

      eth.accounts().then(function(accounts) {
          var myAddr = accounts[0];
          el("#connaddr").innerHTML = myAddr;
          if(myAddr){
            eth.getBalance(myAddr).then(function(valueStored){
              el('#myeth').innerHTML = String(ethUnit.fromWei(valueStored,'ether'));
            });
          }
      });


      el('#zhit').addEventListener('click', function(){
        el('#response').innerHTML = "";
        el("#desc").innerHTML = "日冠军账号，当前直推币数前三";
        show2(0);
      });

      function show2(i){
        if(i>3){
          return;
        } 
        SimpleStore.v16(i).then(function(setTxHash){
            var html = i+":"+String(setTxHash[0])+"<br/>";
            el('#response').innerHTML += html;
            show2(++i);
        });
      }

      el('#zuori').addEventListener('click', function(){
        el('#response').innerHTML = "";
        el("#desc").innerHTML = "日冠军账号，昨日直推币数前三";
        show3(0);
      });

      function show3(i){
        if(i>3){
          return;
        } 
        SimpleStore.v17(i).then(function(setTxHash){
            var html = i+":"+String(setTxHash[0])+"<br/>";
            el('#response').innerHTML += html;
            show3(++i);
        });
      }

      el('#n20').addEventListener('click', function(){
        el('#response').innerHTML = "";
        el("#desc").innerHTML = "溢出计算，需要派发最新20个签约地址，按量排序";
        show(0);
      });

      function show(i){
        if(i>20){
          return;
        } 
        SimpleStore.v23(i).then(function(setTxHash){
            var html = i+":"+String(setTxHash[0]);
            SimpleStore.v23_amt(i).then(function(setTxHash){
              html += ","+String(ethUnit.fromWei(setTxHash[0],'ether'))+"<br/>";
              el('#response').innerHTML += html;
              show(++i);
            });
        });
      }

      el('#setValue').addEventListener('click', function(){
        el("#desc").innerHTML = "合约名称，合约简称";
        SimpleStore.name().then(function(setTxHash){
          el('#response').innerHTML = '名称: ' + String(setTxHash[0]);
        });
        SimpleStore.symbol().then(function(setTxHash){
          el('#response').innerHTML += '，简称: ' + String(setTxHash[0]);
        });
      });

      el('#getValue').addEventListener('click', function(){
        SimpleStore.totalSupply().then(function(valueStored){
          el("#desc").innerHTML = "";
          el('#response').innerHTML = 'totalSupply: ' + String(valueStored[0]);
        });
      });

      
      el('#settle').addEventListener('click', function(){
        el("#desc").innerHTML = "结算，帐户可提币数理增加";
          eth.accounts().then(function(accounts) {
            SimpleStore.settle({from:accounts[0]}).then(function(valueStored,error){
              el('#response').innerHTML = String(valueStored);
            });
          });
      });

      el('#btn_4').addEventListener('click', function(){
        el("#desc").innerHTML = "提币，扣除可提币金额，扣除平台总金额，增加帐户的获得币";
          eth.accounts().then(function(accounts) {
            var v = el("#addr_4").value;
            var l = ethUnit.toWei(v,'ether');
            var g = ethUtil.intToHex(80000);
            // var p = ethUnit.toWei(60,"gwei");
            SimpleStore.drawblock(l,{from:accounts[0],gas:g}).then(function(valueStored,error){
              el('#response').innerHTML = String(valueStored);
            });
          });
      });

      el('#kjs').addEventListener('click', function(){
        el("#desc").innerHTML = "可结算收益";
          eth.accounts().then(function(accounts) {
            SimpleStore.caclSettle({from:accounts[0]}).then(function(valueStored){
              el('#response').innerHTML = String(ethUnit.fromWei(valueStored[0],'ether'));
            });
          });
      });

      el('#btn_6').addEventListener('click', function(){
          el("#desc").innerHTML = "签约";
          eth.accounts().then(function(accounts) {
            var v = el("#addr_6").value;
            if(!v){
              v = "0x0000000000000000000000000000000000000000";
            }
            var amt = el("#amt_6").value;
            var l = ethUnit.toWei(amt,'ether');
            var g = ethUtil.intToHex(1600000);
            var gr = ethUnit.toWei(60,'gwei');
            SimpleStore.join(v,{from:accounts[0],value:l,gas:g,gasPrice:gr}).then(function(valueStored,error){
              el('#response').innerHTML = String(valueStored);
            });
          });
      });


      function isZero(addr){
          if(addr=='0x0000000000000000000000000000000000000000'){
            return true;
          }
          return false;
      }
    </script>
  </body>
</html>
